package com.lb.shopguide.ui.fragment.boss;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lb.shopguide.R;
import com.lb.shopguide.base.BaseMainFragment;
import com.lb.shopguide.event.guide.BackToHomeEvent;
import com.lb.shopguide.event.guide.StartBrotherEvent;
import com.lb.shopguide.event.jump.JumpSecretaryEvent;
import com.lb.shopguide.ui.view.LbBottomBarBoss;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by kang on 2017/12/23.
 */

public class FragmentBossHome extends SupportFragment {
    @BindView(R.id.bottom_bar)
    LbBottomBarBoss lbBottomBar;
    private View view;

    protected Unbinder unbinder;

    private BaseMainFragment[] mFragments = new BaseMainFragment[3];
    public static final int FIRST = 0;
    public static final int SECOND = 1;
    public static final int THIRD = 2;

    private boolean jumpSecFlag = false;


    public static FragmentBossHome newInstance() {
        return new FragmentBossHome();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_boss_main, container, false);
        unbinder = ButterKnife.bind(this, view);
        initData();
        initView();
        return view;
    }

    private void initData() {
        EventBus.getDefault().register(this);
    }

    private void initView() {
        SupportFragment firstFragment = findChildFragment(FragmentMerchant.class);
        if (firstFragment == null) {
            mFragments[FIRST] = FragmentMerchant.newInstance();
            mFragments[SECOND] = FragmentSecretary.newInstance();
            mFragments[THIRD] = FragmentBossMine.newInstance();

            loadMultipleRootFragment(R.id.fl_container, FIRST,
                    mFragments[FIRST],
                    mFragments[SECOND],
                    mFragments[THIRD]);
        } else {
            // 这里我们需要拿到mFragments的引用,也可以通过getChildFragmentManager.getFragments()自行进行判断查找(效率更高些),用下面的方法查找更方便些
            mFragments[FIRST] = findChildFragment(FragmentMerchant.class);
            mFragments[SECOND] = findChildFragment(FragmentSecretary.class);
            mFragments[THIRD] = findChildFragment(FragmentBossMine.class);
        }

        lbBottomBar.setOnTabSelectedListener(new LbBottomBarBoss.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int pos, int prePos) {
                showHideFragment(mFragments[pos], mFragments[prePos]);
            }
        });
        if (jumpSecFlag) {
            lbBottomBar.performClick(LbBottomBarBoss.BOTTOM_SECRETARY);
            jumpSecFlag = false;
        }
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
    }

    /**
     * 启动同级fragment
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void startBrother(StartBrotherEvent event) {
        start(event.targetFragment);
    }

    @Subscribe
    public void backToHome(BackToHomeEvent event) {
        lbBottomBar.performClick(LbBottomBarBoss.BOTTOM_MERCHANT);
    }

    @Subscribe(sticky = true)
    public void jumpToSecretary(JumpSecretaryEvent event) {
        if (getTopFragment() instanceof FragmentBossHome) {
            if (lbBottomBar != null) {
                lbBottomBar.performClick(LbBottomBarBoss.BOTTOM_SECRETARY);
            } else {
                jumpSecFlag = true;
            }
        }
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
        super.onDestroyView();
    }

}
